<?php

require_once('request/AbstractActionRequest.class.php');


class InsertTopicRequest extends AbstractActionRequest {
	
	protected final function internalActionExecute($requestXml) {
		if($requestXml == null) {
			throw new Exception("Insufficient post data!");
			
//			$xml_data = <<<EOR
//<Request>
//	<NeededTopic>
//		<User>
//			<UserId>1</UserId>
//		</User>
//		<Title>Nochmal von zwei</Title>
//		<CategoryId>1</CategoryId>
//	</NeededTopic>
//</Request>
//EOR;
//			$xml_data = <<<EOR
//<Request>
//	<OfferedTopic>
//		<User>
//			<UserId>1</UserId>
//		</User>
//		<Title>Nochmal von zwei</Title>
//		<CategoryId>1</CategoryId>
//	</OfferedTopic>
//</Request>
//EOR;
//			$requestXml = simplexml_load_string($xml_data);
		}
		
		$created = time();
		
		if(isset($requestXml->OfferedTopic)) {
			$xml = $requestXml->OfferedTopic;
			$topic = $this->topicDao->insertOfferedTopic($xml->User->UserId, $xml->Title, $created, $xml->CategoryId);
			
		} else if(isset($requestXml->NeededTopic)) {
			$xml = $requestXml->NeededTopic;
			$topic = $this->topicDao->insertNeededTopic($xml->User->UserId, $xml->Title, $created, $xml->CategoryId);
			
		} else {
			throw new Exception("Invalid XML request data:\n" . $requestXml);
		}
		
		return '<InsertedTopic>' . $topic->toXmlString() . '</InsertedTopic>';
	}
	
}

?>